﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace Feniks
{
    public partial class frmMakine : DevExpress.XtraEditors.XtraForm
    {
        public frmMakine()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cmbMakine.Text) && !string.IsNullOrEmpty(txtAciklama.Text))
            {
                using (DataEntities ent = new DataEntities())
                {
                    var sonuc = ent.MakineTanimlari.Where(x => x.MakineKodu == cmbMakine.Text).FirstOrDefault();
                    if (sonuc != null)
                    {
                        sonuc.MakineTanımı = txtAciklama.Text;
                    }
                    else
                    {
                        ent.AddToMakineTanimlari(new MakineTanim()
                        {
                            MakineKodu = cmbMakine.Text,
                            MakineTanımı = txtAciklama.Text
                        });
                    }
                    ent.SaveChanges();
                }
                cmbMakine.SelectedIndex = -1;
                txtAciklama.Text = "";
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cmbMakine.Text))
            {
                using (DataEntities ent = new DataEntities())
                {
                    var sonuc = ent.MakineTanimlari.Where(x => x.MakineKodu == cmbMakine.Text).FirstOrDefault();
                    if (sonuc != null)
                    {
                        ent.DeleteObject(sonuc);
                        ent.SaveChanges();
                        cmbMakine.SelectedIndex = -1;
                        txtAciklama.Text = "";
                    }
                }
            }
        }

        private void cmbMakine_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbMakine.SelectedIndex > -1)
            {
                MakineTanim item = (MakineTanim)((ComboItem)cmbMakine.SelectedItem).Value;
                if (item != null)
                {
                    txtAciklama.Text = item.MakineTanımı;
                }
            }
        }

        private void frmMakine_Load(object sender, EventArgs e)
        {
            using (DataEntities ent = new DataEntities())
            {
                var liste = ent.MakineTanimlari.ToList();
                foreach (var item in liste)
                {
                    cmbMakine.Properties.Items.Add(new ComboItem()
                    {
                        Text = item.MakineKodu,
                        Value = item
                    });
                }
            }
        }
    }
}